var myApp = sy.getModule('myApp',[]);
myApp.directive('onFinishRenderFilters', function ($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    scope.$emit('ngRepeatFinished',attr.onFinishRenderFilters);
                });
            }
        }
    };
});
myApp.controller('creditController', function($scope,$http,$sce) {
  $scope.login = false;
  $scope.creditless = false;
  sy.openLoad();
  $scope.goods = {};
  $scope.nextPage = function() {
      $http.post("/m/credit/detail",{
    	  		id:_id
      		})
			.success(function (data) {
	            sy.closeLoad();
	            $scope.goods = data;
	            console.log(data);
	            $scope.goods.content = $sce.trustAsHtml(data.content);
	            if (data.imgs){
	            	$scope.images = data.imgs.split(',');
	            }
	        }).error(function(data, status) {
	        	sy.closeLoad();
			});
  };
  $scope.nextPage();
  
  $scope.exchange = function(){
	  if(confirm('您确定要兑换此商品吗？')){
		  sy.openLoad();
		  $http.post("/m/credit/exchange",{
		  		id:_id
			})
			.success(function (data) {
	          sy.closeLoad();
	         if(data=='sessionOut'){
	        	 sy.alert('请先登录!');
	        	 location.href=sy.path+"/m/login";
	        	 return;
	         }
	         if (data.code == -1){
	        	 $scope.creditless = true;
	         }else if (data.code == 1){
	        	 sy.alert('兑换成功');
	         }else{
	        	 sy.alert(data.data);
	         }
	      }).error(function(data, status) {
	      	sy.closeLoad();
			});
	  }
  };
  
  $scope.$on('ngRepeatFinished', function (event,data) {
		if(data == 'img'){
			var swiper = new Swiper('.swiper-container', {
	            pagination: '.swiper-pagination',
	            autoplay : 3000,
	            speed:500,
	            paginationClickable: true,
	            loop:true
	        });
		}
	});
  
});
